In the event of technical difficulties with Szkopuł, please contact us via email at [email protected].
If you would like to talk about tasks, solutions or technical problems, please visit our Discord servers. They are moderated by the community, but members of the support team are also active there.
Byteasar works as a teacher in a primary school in Byteburg. The weather is currently really nice, so Byteasar would like to take his class for a bus trip to Bytetown - the capital of Byteland. Byteasar has decided to hire a travel agency to plan the trip.
The streets in Bytetown form a regular grid of East-West and North-South streets. The distance between any pair of adjacent parallel streets is equal to 1 kilometer. There are tourist attractions located at several junctions. Bytean guides have assigned attractiveness coefficient to each tourist attraction: the greater the attractiveness, the more interesting is the attraction for the visitors. Byteasar knows that the pupils in the class he teaches get bored easily. Because of that he requires that the attractions visited on the trip have increasing attractiveness.
The travel agency has agreed to Byteasar's requirements. In addition, the agency would like to earn as much money as possible for organizing the trip. The agency gets a fixed rate of 1 bythaler for each kilometer of the trip. The bus always chooses the shortest route along the streets of Bytetown when driving between the attractions on the trip. Moreover the agency gets additional money from the managers of attractions that are visited along the trip.
Help the agency plan a trip that satisfies Byteasar's requirements and grants the agency the highest profit. Please note that driving next to a tourist attraction (without stopping) does not count as visiting the attraction.
The first line of input contains two integers and (), the number of East-West streets and the number of North-South streets.
lines follow with a description of the tourist attractions in Bytetown. The -th of these lines holds integers () that give the attractiveness of each of the attractions located at the junctions of the -th East-West street with the respective North-South streets. Attractiveness means that there is no tourist attraction at the respective junction. You can assume that there is at least one tourist attraction in Bytetown.
Each of the following lines holds integers (). The number , that is, the -th number in the -th of the considered lines, represents the amount of money (in bythalers) that the agency receives for making a trip lead through the tourist attraction described by the attractiveness . If there is no tourist attraction at a junction, the corresponding number equals .
The only line of output should contain one integer: the maximum profit (in bythalers) that the agency can make for organizing a trip leading through a number of attractions with strictly increasing attractiveness.
For the input data:
4 5 1 2 6 0 2 1 3 4 0 4 0 0 4 0 3 2 2 0 0 4 1 3 5 0 2 2 8 1 0 2 0 0 3 0 4 0 5 0 0 3
the correct result is:
39
Explanation of the example:
The numbers in the junctions represent the attractiveness of the respective attractions.
The numbers written in italic represent the profit of the travel agency for making
a trip lead through the respective attractions.
The most profitable trip for the agency is highlighted with circles:
the agency receives , , , and bythalers for these attractions respectively,
and moreover it receives bythalers as the bus fare.
Task author: Jakub Radoszewski.